import { Controller } from "../../mvc/core/Controller";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { GameProxy } from "../mvc/GameProxy";
import { DataEvent } from "../events/DataEvent";
import { ECategory } from "../../../Message/Public/ECategory";
import { EProp } from "../../../Message/Public/EProp";
import { QuickBuyWindow } from "../view/common/QuickBuyWindow";
import { ShopRule } from "../rules/ShopRule";
import { ShopConfig } from "../resource/ShopConfig";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { CycleInfo } from "../model/CycleInfo";
import { ICycleModule } from "../../../modules/interfaces/ICycleModule";
import { ObjCreate } from "../view/common/ObjCreate";
import { ETaskGroup } from "../../../Message/Game/ETaskGroup";
import { ETaskStatus } from "../../../Message/Game/ETaskStatus";
import { WindowEvent } from "../../component/window/WindowEvent";
import { IView } from "../../mvc/interfaces/IView";
import { CycleModule } from "../../../modules/CycleModule";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
type int = number;
//class CycleController
    
    export  class CycleController extends Controller
    {
       

        /* internal  */cycleTaskReq(): void
        {
            if (cache.daily.loopBookInfo.needLevel > cache.role.entityInfo.level) 
            {
                MsgManager.showRollTipsMsg(Language.getStringByParam(20036, cache.daily.loopBookInfo.needLevel));
                return;
            }
            if (cache.daily.loopBookInfo.num == cache.daily.loopBookInfo.totalNum) 
            {
                MsgManager.showRollTipsMsg(Language.getString(20037));
                return;
            }
            if (!view.isHide) 
            {
                GameProxy.cycle.loopTaskReq();
            }
            view.show();
            return;
        }

        /* internal  */onReflashColorReq(arg1: DataEvent): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=null;
            if (cache.cycle.cycleInfo.freeCount == 0 && cache.cycle.cycleInfo.color < 5) 
            {
                loc1 = cache.pack.backPackCache.getTypeItems(ECategory._ECategoryProp, EProp._EPropColorStone);
                if (!loc1 || loc1.length == 0) 
                {
                    if (!cache.cycle.cycleInfo.autoBuy) 
                    {
                        loc2 = QuickBuyWindow.instance;
                        loc2.updateWindow(ECategory._ECategoryProp, EProp._EPropColorStone, 1, -1, false, ShopRule.FishShop);
                        loc2.show();
                        return;
                    }
                    if (this._colorStoneCoin == -1) 
                    {
                        if (loc3 = ShopConfig.instance.getItemByType(ECategory._ECategoryProp, EProp._EPropColorStone, 1, -1, false, ShopRule.FishShop)) 
                        {
                            this._colorStoneCoin = loc3.tshopSell.price;
                        }
                    }
                    if (cache.login.loginGame.money.coin + cache.login.loginGame.money.coinBind < this._colorStoneCoin) 
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallAutoBuyCoin, this._colorStoneCoin));
                        MsgManager.showRollTipsMsg(Language.getString(20038));
                        return;
                    }
                }
            }
            GameProxy.cycle.colorReflashReq(cache.cycle.cycleInfo.autoBuy, cache.cycle.cycleInfo.flushCount, cache.cycle.cycleInfo.flushColor);
            return;
        }

        /* internal  */onTaskGetReq(arg1: DataEvent): void
        {
            GameProxy.cycle.taskGetReq();
            return;
        }

        /* internal  */onTaskEndReq(arg1: DataEvent): void
        {
            GameProxy.cycle.taskEndReq();
            return;
        }

        /* internal  */onMoveAwardReq(arg1: DataEvent): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.CycleTaskMoveAward, cache.cycle.cycleInfo));
            return;
        }

        /* internal  */onCycleTaskInfoReq(arg1: DataEvent): void
        {
            if (_view && !_view.isHide) 
            {
                GameProxy.cycle.loopTaskReq();
            }
            return;
        }

        /* internal  */onCycleTaskRes(arg1: CycleInfo): void
        {
            if (!view.isHide) 
            {
                (view as ICycleModule).updateCycleInfo(arg1);
            }
            return;
        }

        /* internal  */onCycleTaskEnd(arg1: ObjCreate): void
        {
            if (!view.isHide) 
            {
                (view as ICycleModule).endCycleCount();
            }
            return;
        }

        /* internal  */onColorFlushRes(arg1: CycleInfo): void
        {
            if (!view.isHide) 
            {
                (view as ICycleModule).updateColor(arg1);
            }
            return;
        }

        /* internal  */onTaskGetRes(arg1: CycleInfo): void
        {
            if (!view.isHide) 
            {
                (view as ICycleModule).getCycle(arg1);
            }
            return;
        }

        /* internal  */onTaskUpdateRes(arg1: Array<any>): void
        {
            var loc1=0;
            var loc3=null;
            var loc4=null;
            var loc2=arg1.length;
            while (loc1 < loc2) 
            {
                if ((loc3 = arg1[loc1]).sTask.group == ETaskGroup._ETaskGroupLoopBook) 
                {
                    loc4 = cache.cycle.updateTaskProcess(loc3);
                    if (loc3.status != ETaskStatus._ETaskStatusHadCompleted) 
                    {
                        if (_view && !_view.isHide && !(loc4 == null)) 
                        {
                            (view as ICycleModule).updateTaskProcess(loc4);
                        }
                    }
                    else if (view.isHide || loc4 == null) 
                    {
                        view.show();
                    }
                    else 
                    {
                        (view as ICycleModule).updateTaskProcess(loc4);
                    }
                    break;
                }
                ++loc1;
            }
            return;
        }

        /* internal  */onTaskCancelRes(arg1: int): void
        {
            if (_view && !_view.isHide) 
            {
                _view.hide();
            }
            return;
        }

        /* internal  */onUseFlushLoopBook(arg1: DataEvent): void
        {
            this.cycleTaskReq();
            return;
        }

        /* internal  */onUsePropColorStoneHandler(arg1: DataEvent): void
        {
            var loc1=cache.pack.backPackCache.getTypeItems(ECategory._ECategoryProp, EProp._EPropTaskLoopBook);
            if (loc1.length < 0) 
            {
                MsgManager.showRollTipsMsg(Language.getString(20039));
            }
            this.cycleTaskReq();
            return;
        }

        /* internal  */onTaskListRes(arg1: Array<any>): void
        {
            var loc1=0;
            var loc3=null;
            var loc4=null;
            var loc2=arg1.length;
            while (loc1 < loc2) 
            {
                if ((loc3 = arg1[loc1]).sTask.group == ETaskGroup._ETaskGroupLoopBook && loc3.status == ETaskStatus._ETaskStatusHadCompleted) 
                {
                    if (!((loc4 = cache.pack.backPackCache.getTypeItems(ECategory._ECategoryProp, EProp._EPropTaskLoopBook)).length < 0)) 
                    {
                        view.show();
                    }
                }
                ++loc1;
            }
            return;
        }

        /* internal  */onWindowShow(arg1: WindowEvent): void
        {
            this.cycleTaskReq();
            return;
        }

        protected /* override */ initView(): IView
        {
            var loc1=new CycleModule();
            loc1.addEventListener(WindowEvent.SHOW, this.onWindowShow);
            loc1.addEventListener(EventName.CycleReflashColor, this.onReflashColorReq);
            loc1.addEventListener(EventName.CycleGetTaskReq, this.onTaskGetReq);
            loc1.addEventListener(EventName.CycleEndTaskReq, this.onTaskEndReq);
            loc1.addEventListener(EventName.CycleTaskMoveAward, this.onMoveAwardReq);
            return loc1;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(ServerCommand.CycleTaskRes, this.onCycleTaskRes);
            NetDispatcher.addCmdListener(ServerCommand.CycleTaskEnd, this.onCycleTaskEnd);
            NetDispatcher.addCmdListener(ServerCommand.CycleColorFlushRes, this.onColorFlushRes);
            NetDispatcher.addCmdListener(ServerCommand.CycleTaskGetRes, this.onTaskGetRes);
            NetDispatcher.addCmdListener(ServerCommand.TaskUpdate, this.onTaskUpdateRes);
            NetDispatcher.addCmdListener(ServerCommand.CycleTaskCancelRes, this.onTaskCancelRes);
            NetDispatcher.addCmdListener(ServerCommand.TaskListRes, this.onTaskListRes);
            Dispatcher.addEventListener(EventName.UseTaskFlushLoopBook, this.onUseFlushLoopBook);
            Dispatcher.addEventListener(EventName.CycleTaskInfoReq, this.onCycleTaskInfoReq);
            Dispatcher.addEventListener(EventName.UsePropColorStone, this.onUsePropColorStoneHandler);
            return;
        }

        private /* var */_colorStoneCoin: int=-1;
    }
